package ms.mall.service;

import ms.common.exception.BizException;
import ms.common.util.bean.BeanUtil;
import ms.mall.bean.CartParamBean;
import ms.mall.config.ResponseCode;
import ms.mall.dao.CartDAO;
import ms.mall.dao.UserDAO;
import ms.mall.entity.Cart;
import ms.mall.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Project Name: ms
 * File Name: CartService
 * Package Name: ms.mall.service
 * Date: 2018/6/4  16:50
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class CartService {

    @Autowired
    private CartDAO cartDAO;

    @Autowired
    private UserDAO userDAO;

    /**
     * 加入购物车
     * @param cartParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void create(CartParamBean cartParamBean) {
        User user = userDAO.selectByPrimaryKey(cartParamBean.getOpenid());
        if(null == user) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "用户不存在");
        }

        Cart cartParam = new Cart();
        cartParam.setSkuId(cartParamBean.getSkuId());
        cartParam.setStatus(2);

        Cart existCart = cartDAO.selectOne(cartParam);
        if(null == existCart) {//没有
            Cart cart = BeanUtil.copy(cartParamBean, Cart.class);
            cart.setStatus(2);
            cart.setCreateTime(System.currentTimeMillis());
            cart.setOrderId("");

            cartDAO.insert(cart);
            return;
        }

        existCart.setNum(existCart.getNum() + cartParamBean.getNum());
        cartDAO.updateByPrimaryKeySelective(existCart);
    }

    /**
     * 修改购物车
     * @param cartParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void update(CartParamBean cartParamBean) {
        User user = userDAO.selectByPrimaryKey(cartParamBean.getOpenid());
        if(null == user) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "用户不存在");
        }

        Cart currentCart = cartDAO.selectByPrimaryKey(cartParamBean.getId());
        if(null == currentCart) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "购物车不存在");
        }

        Cart cart = new Cart();
        cart.setId(cartParamBean.getId());
        cart.setNum(cartParamBean.getNum());

        cartDAO.updateByPrimaryKeySelective(cart);
    }

    /**
     * 删除购物车
     * @param cartParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void del(CartParamBean cartParamBean) {
        User user = userDAO.selectByPrimaryKey(cartParamBean.getOpenid());
        if(null == user) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "用户不存在");
        }

        Cart currentCart = cartDAO.selectByPrimaryKey(cartParamBean.getId());
        if(null == currentCart) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "购物车不存在");
        }

        Cart cart = new Cart();
        cart.setId(cartParamBean.getId());
        cart.setStatus(1);

        cartDAO.updateByPrimaryKeySelective(cart);
    }

    /**
     * 购物车列表
     * @param cartParamBean
     */
    public List<Cart> list(CartParamBean cartParamBean) {
        Cart cart = new Cart();
        cart.setOpenid(cartParamBean.getOpenid());
        cart.setStatus(2);

        return cartDAO.selectByExample(cart);
    }
}
